﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Security.Cryptography;
using System.Data;
using System.Text.RegularExpressions;

namespace mamnontuoitho7.module
{
    public partial class ProcessLogin : System.Web.UI.Page
    {
        private mamnontuoitho7.AppCode.Article _article = new mamnontuoitho7.AppCode.Article();
        private Member _member = new Member();
        private Parent _parent = new Parent();
        private General _general = new General();

        protected string _notice = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["action"] != null && Request.QueryString["action"].ToString().Equals("logout"))
            {
                Session["username"] = "";
                Session["role"] = "";
                Session["memberID"] = "";
                Session["avatar"] = "";
                Session["fullname"] = "";
                Session["classID"] = "";

                if (Session["Common_currentPageLink"] == null)
                {
                    Response.Redirect(ConstantClass._errorLoginPage);
                }
                else
                {
                    Response.Redirect(Session["Common_currentPageLink"].ToString());
                }
            }


            try
            {


                if (!IsPostBack)
                {


                    if (Request.Form["txtUsername"] != null && Request.Form["txtPassword"] != null)
                    {
                        if (!Request.Form["txtUsername"].ToString().Equals("") && !Request.Form["txtUsername"].ToString().Equals("txtPassword"))
                        {
                            MemberObjectForAuthorrization _currentUser = _member.checkLogin(Request.Form["txtUsername"].ToString().Trim(), _general.ComputeHash(Request.Form["txtPassword"].ToString().Trim(), new SHA256CryptoServiceProvider()));
                            if (_currentUser.IsAuthorized && _currentUser.MemberVisible)
                            {
                                Session["username"] = _currentUser.username;
                                Session["role"] = _currentUser.RoleID;
                                Session["memberID"] = _currentUser.MemberID;
                                Session["avatar"] = _currentUser.Avatar;
                                Session["fullname"] = _currentUser.Fullname;
                                Session["classID"] = _currentUser.ClassID;

                                if (_currentUser.RoleID.ToString() == "4")
                                {
                                    if (_parent.CheckParentAvailability(_currentUser.MemberID) == "2")
                                    {
                                        DataTable _dtClass = _member.GetClassByParent(_currentUser.MemberID);
                                        string strClassID = "", strtemp = "";
                                        int a = 0;
                                        if (_dtClass != null)
                                        {
                                            a = _dtClass.Rows.Count;
                                        }

                                        for (int i = 0; i < a; i++)
                                        {
                                            if (i == a - 1)
                                            {
                                                strtemp = _dtClass.Rows[i]["ClassID"].ToString();
                                            }
                                            else
                                            {
                                                strtemp = _dtClass.Rows[i]["ClassID"].ToString() + ",";
                                            }
                                            strClassID += strtemp;
                                            Session["strClassID"] = strClassID;
                                        }
                                    }

                                }

                                if (Session["Common_currentPageLink"] == null)
                                {
                                    Response.Redirect(ConstantClass._errorLoginPage);
                                }
                                else
                                {
                                    Response.Redirect(Session["Common_currentPageLink"].ToString());
                                }
                            }
                            else
                            {

                                Response.Redirect(ConstantClass._errorLoginPage + "?notice=Tên đăng nhập hoặc mật khẩu không chính xác.");
                            }
                        }
                        else
                        {
                            Response.Redirect(ConstantClass._errorLoginPage + "?notice=Đăng nhập không hợp lệ");
                        }
                    }
                    else
                    {
                        Response.Redirect(ConstantClass._errorLoginPage);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Write(ConstantClass._errorLoginPage + "?notice=Có lỗi hệ thống hoặc mạng.\nXin vui lòng đăng nhập lại.");
            }

        }
    }
}